package com.example.bigevent.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.bigevent.pojo.Article;
import com.example.bigevent.pojo.Category;
import com.example.bigevent.pojo.PageBean;
import com.example.bigevent.pojo.Result;
import com.example.bigevent.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-30
 */
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    /**
     * 新增文章；！！校验state无效！！
     * @param article
     * @return
     *
     */
    @PostMapping
    public Result add(@RequestBody Article article){
        articleService.addArticle(article);
        return Result.success();
    }

    /**
     * 文章列表(条件分页)
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> listArticle(
            @RequestParam(defaultValue = "0") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> list = articleService.listArticle(pageNum,pageSize,categoryId,state);
        return Result.success(list);
    }
    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> categoryDetail(Integer id){
        return Result.success(articleService.getById(id));
    }
    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.updateArticle(article);
        return Result.success();
    }
    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        articleService.removeById(id);
        return Result.success();
    }
}
